define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, $timeout, Creditrepayview, GoDialog, $stateParams, GoPage, $window,$ionicSlideBoxDelegate, $ionicModal) {
		
		$scope.selectIndex = 0;
		$scope.type = "repay";//默认收款
		$scope.datasArr = new Array();//存放一览数据
		$scope.nPageArr = [1,1];//页数
		
		$scope.$on('$ionicView.afterEnter', function(){
			var lan = 0; //这是状态栏，只有ios的APP才有
			if(window.cordova && window.cordova.plugins && ionic.Platform.isIOS()) { 
				lan = 20; 
			}
			var all = $window.innerHeight;
			$scope.myObj = {
				"height": all - 44*2 - lan + "px"
			};
			$scope.id = $stateParams.id;
			$scope.init_data();
		},false)
		
		//切换页面
		$scope.activeSlide = function(index) {
			$scope.selectIndex = index;
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').slide($scope.selectIndex);
			if($scope.selectIndex==0){
				$scope.type = "repay";//收款：repay
			}else{
				$scope.type = "pay";//欠款：pay
			}
			if(!$scope.datasArr[$scope.selectIndex]){
				$scope.init_data();
			}
		};
		$scope.slideChanged = function(index) {
			$scope.selectIndex = index;
		};
		//禁止左右滑动来切换页面1
		$scope.onTouch = function() {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(false);
		};
		//禁止左右滑动来切换页面2
		$scope.onRelease = function() {
			$ionicSlideBoxDelegate.$getByHandle('tab-handle').enableSlide(true);
		};
		
		//初始化。type repay：收款，pay：欠款
		$scope.init_data = function() {
			Creditrepayview.agent_credit(1, $scope.id, $scope.type, function(data) {
//				console.log(data)
				$scope.datasArr[$scope.selectIndex] = data;
			})
		}
		
		//确认还款/作废。cancel：0确认  1作废
		$scope.confrim = function(member_id, info_id, money, cancel) {
			var msg = "确定已到账？";
			if(cancel==1){
				msg = "确定作废？";
			}
			GoDialog.confirm("提示",msg,function(){
				Creditrepayview.confrim_credit(member_id, info_id, money,cancel,function(data) {
					if(data.success) {
						if(cancel==1)
							GoDialog.toast("作废成功");
						else
							GoDialog.toast("收款成功");
						$scope.init_data();
					} else{
						GoDialog.alert("提示",data.msg);
					}
				})
			})
		}
		
		/***************查看凭证**************/
		$ionicModal.fromTemplateUrl("templates/modal/credit_voucher.html", {
			scope: $scope,
			animation: "slide-in-up"
		}).then(function(modal) {
			$scope.modal_voucher = modal;
		});
		//打开
		$scope.openModal_voucher = function(index) {
			var data = $scope.datasArr[$scope.selectIndex].credit[index];
			if(!data) return;
			var repayment = data.repayment;
			$scope.voucher = data.voucher;
			$scope.voucher.repayment = repayment;
			$scope.modal_voucher.show();
		}
		//隐藏
		$scope.closeModal_voucher = function() {
			$scope.modal_voucher.hide();
		}
		/***************查看凭证**************/
		
		
		/***************上下拉刷新*****************/
		$scope.isLoad = false;
		$scope.nPage = 1;
		$scope.doRefresh = function() {
			//防止重复加载
			$scope.isLoad = true;
			Creditrepayview.agent_credit(1, $scope.id, $scope.type, function(data) {
				$scope.datasArr[$scope.selectIndex] = data;
				$scope.nPage = 1;
			}, function() {
				$scope.$broadcast('scroll.refreshComplete');
				$timeout(function() {
					$scope.isLoad = false; //可以加载
				}, 1500);
			});
		}
		$scope.doRefreshMore = function() {
			//防止重复加载
			$scope.isLoad = true;
			Creditrepayview.agent_credit($scope.nPageArr[$scope.selectIndex]+1, $scope.id, $scope.type, function(data) {
				if(data!=null) {
					if($scope.datasArr[$scope.selectIndex] != undefined) {
						$scope.datasArr[$scope.selectIndex].credit = $scope.datasArr[$scope.selectIndex].credit.concat(data.credit);
						$scope.nPageArr[$scope.selectIndex] += 1;
//							console.log($scope.nPageArr)
					} else {
						$scope.datasArr[$scope.selectIndex] = data;
					}
				}
			}, function() {
				$scope.$broadcast('scroll.infiniteScrollComplete');
				$timeout(function() {
					$scope.isLoad = false;
				}, 1500);
			});
		}
		/***************上下拉刷新*****************/
		
		//查看凭证
		$scope.showBigImg = function(imgUrl) {
			$scope.closeModal_voucher();
			GoDialog.showBigImg(imgUrl);
		}
		
		
	}
	controller.$inject = ['$scope', '$timeout', 'Creditrepayview', 'GoDialog', '$stateParams', 'GoPage', '$window', '$ionicSlideBoxDelegate', '$ionicModal'];

	return app.registerController("creditrepayviewctrl",controller);
});